import { Navigate, useEffect, useState } from 'react-router-dom'
import axios from 'axios'
// //权限判断
// export const RouterAuth = ({ children }) => {
//     const token = localStorage.getItem('token')
//     if (!token) {
//         return <Navigate to='/login' replace />
//     }
//     return (
//         children
//     )
// }

// eslint-disable-next-line react/prop-types
export const RouterAuth = ({ children }) => {
  const [loading, setLoading] = useState(true)
  const [hasAuth, setHasAuth] = useState(false)
  useEffect(() => {
    axios.get('/permission/getMenu').then(({ data }) => {
      setLoading(false)
      setHasAuth(true)
    }).catch(() => {
      setLoading(false)
      setHasAuth(false)
    })
  }, [])
  if (loading) {
    // eslint-disable-next-line react/react-in-jsx-scope
    return <div>加载中...</div>
  }
  // eslint-disable-next-line react/react-in-jsx-scope
  return hasAuth ? children : <Navigate to='/login' replace />
}
